
import 'package:get/get.dart';
import 'names.dart';
import '../../pages/index.dart';
import 'middlewares/index.dart';
import 'observer.dart';

/// 路由页面配置类
/// 
/// 🔐 认证中间件使用规则：
/// 
/// ✅ 无需登录的页面模块：
/// - System 系统模块（登录、注册、主页、闪屏）
/// - Home 首页模块（展示类页面）
/// - Discover 发现模块（浏览类页面）
/// - Search 搜索模块（搜索功能）
/// - Test 测试模块（调试功能）
/// 
/// 🔒 需要登录的页面模块：
/// - Message 消息模块（用户私人消息）
/// - My 我的模块（个人信息管理）
/// - Order 订单模块（用户订单数据）
class RoutePages {
  // 路由观察者
  static final AppRouteObserver observer = AppRouteObserver();
  
  // 路由历史记录
  static final List<String> history = <String>[];
  
  // 历史记录最大长度
  static const int maxHistoryLength = 20;
  
  /// 添加路由历史记录
  /// 
  /// [route] 路由名称
  static void addHistory(String? route) {
    if (route != null && route.isNotEmpty) {
      // 如果是重复的最后一个路由，不添加
      if (history.isNotEmpty && history.last == route) {
        return;
      }
      
      history.add(route);
      
      // 如果超出最大长度，移除最早的记录
      if (history.length > maxHistoryLength) {
        history.removeAt(0);
      }
    }
  }
  
  /// 获取上一个路由
  static String? get previousRoute {
    if (history.length < 2) return null;
    return history[history.length - 2];
  }
  
  /// 获取当前路由
  static String? get currentRoute {
    if (history.isEmpty) return null;
    return history.last;
  }
  
  /// 清除历史记录
  static void clearHistory() {
    history.clear();
  }

  static List<GetPage> list = [
    // System 系统模块 - 无需登录
    // 注意：登录和注册页面使用底部往上的滑动动画
    GetPage(
      name: RouteNames.systemSplash,
      page: () => const SplashPage(),
      binding: SplashBinding(),
    ),
    GetPage(
      name: RouteNames.systemLogin,
      page: () => const LoginPage(),
      binding: LoginBinding(),
      transition: Transition.downToUp, // 从底部往上显示动画
      transitionDuration: const Duration(milliseconds: 300), // 动画持续时间
    ),
    GetPage(
      name: RouteNames.systemRegist,
      page: () => const RegistPage(),
      binding: RegistBinding(),
      transition: Transition.downToUp, // 从底部往上显示动画
      transitionDuration: const Duration(milliseconds: 300), // 动画持续时间
    ),
    GetPage(
      name: RouteNames.systemMain,
      page: () => const MainPage(),
      binding: MainBinding(),
    ),
    GetPage(
      name: RouteNames.systemMap,
      page: () => const MapPage(),
      // 🔥 移除 Binding，Controller 在 View 中直接实例化（学习 playtrue3）
    ),
    GetPage(
      name: RouteNames.systemAmap,
      page: () => const AmapPage(),
      binding: AmapBinding(),
    ),
    
    // AI 模块 - 无需登录
    GetPage(
      name: RouteNames.aiIndex,
      page: () => const AiPage(),
      binding: AiBinding(),
    ),
    
    // Home 首页模块 - 无需登录
    GetPage(
      name: RouteNames.homeHomeIndex,
      page: () => const HomeIndexPage(),
      binding: HomeIndexBinding(),
    ),
    GetPage(
      name: RouteNames.homeHomeDetial,
      page: () => const HomeDetialPage(),
      binding: HomeDetialBinding(),
    ),
    
    // Message 消息模块 - 需要登录
    GetPage(
      name: RouteNames.messageMessageIndex,
      page: () => const MessageIndexPage(),
      binding: MessageIndexBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.messageMessageDetial,
      page: () => const MessageDetialPage(),
      binding: MessageDetialBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.messageMessageChat,
      page: () => const MessageChatPage(),
      binding: MessageChatBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    
    // My 我的模块 - 需要登录
    GetPage(
      name: RouteNames.myMyIndex,
      page: () => const MyIndexPage(),
      binding: MyIndexBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.myPeofileDetial,
      page: () => const PeofileDetialPage(),
      binding: PeofileDetialBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.myPrivacyPolicy,
      page: () => const PrivacyPolicyPage(),
      binding: PrivacyPolicyBinding(),
    ),
    GetPage(
      name: RouteNames.myFeedback,
      page: () => const FeedbackPage(),
      binding: FeedbackBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.myFunds,
      page: () => const FundsPage(),
      binding: FundsBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.myReports,
      page: () => const ReportsPage(),
      binding: ReportsBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.myRules,
      page: () => const RulesPage(),
      binding: RulesBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.mySecurity,
      page: () => const SecurityPage(),
      binding: SecurityBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.mySettings,
      page: () => const SettingsPage(),
      binding: SettingsBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.myUserInfo,
      page: () => const UserInfoPage(),
      binding: UserInfoBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.myVerify,
      page: () => const VerifyPage(),
      binding: VerifyBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    
    // Order 订单模块 - 需要登录
    GetPage(
      name: RouteNames.orderOrderIndex,
      page: () => const OrderIndexPage(),
      binding: OrderIndexBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.orderOrderDetial,
      page: () => const OrderDetialPage(),
      binding: OrderDetialBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    
    // Discover 发现模块 - 无需登录
    GetPage(
      name: RouteNames.discoverDiscoverIndex,
      page: () => const DiscoverIndexPage(),
      binding: DiscoverIndexBinding(),
    ),
    GetPage(
      name: RouteNames.discoverDiscoverDetial,
      page: () => const DiscoverDetialPage(),
      binding: DiscoverDetialBinding(),
    ),
    
    // Search 搜索模块 - 无需登录
    GetPage(
      name: RouteNames.searchSearchIndex,
      page: () => const SearchIndexPage(),
      binding: SearchIndexBinding(),
    ),
    GetPage(
      name: RouteNames.searchSearchFilter,
      page: () => const SearchFilterPage(),
      binding: SearchFilterBinding(),
    ),
    
    // Test 测试模块 - 无需登录
    GetPage(
      name: RouteNames.testTestIndex,
      page: () => const TestIndexPage(),
      binding: TestIndexBinding(),
    ),
    GetPage(
      name: RouteNames.testTestPermission,
      page: () => const TestPermissionPage(),
      binding: TestPermissionBinding(),
      transition: Transition.downToUp, // 从底部往上显示动画
    ),
    GetPage(
      name: RouteNames.testNetworkDiagnostics,
      page: () => const NetworkDiagnosticsPage(),
      binding: NetworkDiagnosticsBinding(),
      transition: Transition.downToUp, // 从底部往上显示动画
    ),
    GetPage(
      name: RouteNames.testSharedPreferences,
      page: () => const SharedPreferencesPage(),
      binding: SharedPreferencesBinding(),
      transition: Transition.downToUp, // 从底部往上显示动画
    ),
    
    // Activity 活动模块 - 需要登录
    GetPage(
      name: RouteNames.activityActiviteHistory,
      page: () => const ActiviteHistoryPage(),
      binding: ActiviteHistoryBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.activityActivitePubilsh,
      page: () => const ActivitePubilshPage(),
      binding: ActivitePubilshBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.activityApplicationMangement,
      page: () => const ActivityApplicationMangementPage(),
      binding: ActivityApplicationMangementBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.activityActivityDetial,
      page: () => const ActivityDetialPage(),
      binding: ActivityDetialBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.activityActivityIndex,
      page: () => const ActivityIndexPage(),
      binding: ActivityIndexBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.activityActivityEdit,
      page: () => const ActivityEditPage(),
      binding: ActivityEditBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.activityActivityManage,
      page: () => const ActivityManagePage(),
      binding: ActivityManageBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
    GetPage(
      name: RouteNames.activityMyPublish,
      page: () => const MyPublishPage(),
      binding: MyPublishBinding(),
      middlewares: [AuthMiddleware()], // 🔒 需要登录
    ),
  ];
}
